<?php
#基础函数
include_once('_path.php');

#db
include_once(_CONST_LOCAL_INCLUDE_PATH."database.class.php");
$database = new Database($db_vars);

if ($db_slave_vars['hostname'] != '') {
	$slave = new Database($db_slave_vars);
} else {
	$slave =& $database;
}

#通用函数
include_once(_CONST_LOCAL_INCLUDE_PATH."common.php");

#处理缩略图
include_once(_CONST_LOCAL_INCLUDE_PATH.'phpthumb/ThumbLib.inc.php');

#weibo
include_once(_CONST_LOCAL_INCLUDE_PATH."/weibo/saetv2.ex.class.php");

$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );

if (isset($_REQUEST['code'])) {
	$keys = array();
	$keys['code'] = $_REQUEST['code'];
	$keys['redirect_uri'] = WB_CALLBACK_URL;
	try {
		$token = $o->getAccessToken( 'code', $keys ) ;
	} catch (OAuthException $e) {
	}
}

if($token)
{
	$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $token['access_token'] );
	$uid_get = $c->get_uid();
	$uid = $uid_get['uid'];
	if($uid>0){
		#判断是否已经注册
		$rs = $database->queryFirst("SELECT id, name FROM user WHERE wb_id=".string_sql($uid)." AND wb_type=".string_sql('sina'));
		if(!empty($rs['id']))
		{
			$_SESSION['userid'] = $rs['id'];
			$_SESSION['name'] = $rs['name'];
		}
		else
		{
			#获得注册信息进行注册
			$user_info = $c->show_user_by_id($uid);
			
			$name = string_sql($user_info['screen_name']);
			$email = string_sql('');
			$lasttime = string_sql(_CONST_TIMENOW);
			$lastip = string_sql(fetch_alt_ip());
			$wb_id = string_sql($uid);
			$wb_type = string_sql('sina');			
			
		    $database->execute("INSERT INTO user (name, email, lasttime, lastip, wb_id, wb_type) VALUES ("
			.$name." ,".$email." ,"
			.$lasttime." ,".$lastip." ,".$wb_id." ,".$wb_type
			.");");
			
			if ($id = $database->getInsertId()) {
				$_SESSION['userid'] = $id;
				$_SESSION['name'] = $user_info['screen_name'];
			}			
			
			#同步头像
			$uploadpath = _CONST_PIC_PATH . 'avatar/' . $_SESSION['userid'] . "/";
			if(!is_dir($uploadpath)) {
				@mkdir($uploadpath);
			}
			
			$uploadfile = $user_info['avatar_large'];
			
			$intro = string_sql($user_info['description']);
			$sex = $user_info['gender'];
			if($sex=='m'){
			$sex=1;
			}elseif($sex=='f')
			{
			$sex=2;
			}else{
			$sex=0;
			}
			$user_id = $_SESSION['userid'];
			
			if(!empty($uploadfile)){
				$basename = md5($uploadfile) . '.jpg';
				$options = array('resizeUp' => true, 'jpegQuality' => 100);
				
				$thumb1 = PhpThumbFactory::create($uploadfile, $options);
				$thumb1->adaptiveResize(30, 30)->save($uploadpath . '1_' . $basename);
				$thumb2 = PhpThumbFactory::create($uploadfile, $options);
				$thumb2->adaptiveResize(50, 50)->save($uploadpath . '2_' . $basename);
				$thumb3 = PhpThumbFactory::create($uploadfile, $options);
				$thumb3->adaptiveResize(180, 180)->save($uploadpath . '3_' . $basename);
	
				$avatar = string_sql($basename);
				#更新头像信息
				$database->execute("INSERT INTO user_info (user_id, sex, intro, avatar) VALUES (".$user_id.",".$sex.",".$intro.",".$avatar.");");
			}
			else{
			    $database->execute("INSERT INTO user_info (user_id, sex, intro) VALUES (".$user_id.",".$sex.",".$intro.");");
			}
		}
	}
}
header("Location:" . _CONST_WEB_URL . 'index.php');
exit;
?>